package com.hedl.studyandjava8.java8_2unit.java8_25;

import com.hedl.studyandjava8.java8_2unit.java8_25.entity.Apple;
import com.hedl.studyandjava8.java8_2unit.java8_25.entity.Color;
import com.hedl.studyandjava8.java8_2unit.java8_25.service.Predicate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author Big Watermelon
 * @description
 * @date 2024/02/19/18:53
 */
public class LambdaMain {
    public static <T> List<T> filter(List<T> arrList, Predicate<T> p){
        if (arrList.size() == 0){
            throw new RuntimeException("集合不能为null");
        }
        List<T> resultList = new ArrayList<>();

        for (T t : arrList) {
            if (p.test(t)){
                resultList.add(t);
            }
        }
        return resultList;
    }

    public static void main(String[] args) {
        List<Apple> appleList = Arrays.asList(
                new Apple(100, Color.RED),
                new Apple(12, Color.GREEN),
                new Apple(150, Color.RED),
                new Apple(50, Color.RED),
                new Apple(180, Color.RED),
                new Apple(200, Color.GREEN),
                new Apple(59, Color.GREEN),
                new Apple(160, Color.RED),
                new Apple(110, Color.GREEN));

        //List<Apple> filter = filter(appleList, (Apple a) -> Color.RED.equals(a.getColor()));
        List<Apple> filter = filter(appleList, (Apple a) -> a.getWeight() % 2 == 0);
        System.out.println(filter.size());

        //实现例子1
        //匿名类的写法
        /*
        appleList.sort(new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getWeight().compareTo(o2.getWeight());
            }
        });
        System.out.println(appleList);

         */

        System.out.println("-----------------------------------");
        //lambda写法
        appleList.sort((Apple a1,Apple a2)->a1.getWeight().compareTo(a2.getWeight()));
        System.out.println(appleList);

        //实现例子2
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello 大西瓜！");
            }
        });
        thread.start();

        Thread t = new Thread(() -> System.out.println("你好呀，大西瓜。你在学习lambda的写法格式吗？"));
        t.start();

        //实现例子2
        System.out.println("-----------------------------------");
        ExecutorService executorService = Executors.newCachedThreadPool();
        /*
        Future<String> future = executorService.submit(new Callable<String>() {

            @Override
            public String call() throws Exception {
                Thread thread1 = new Thread("aaa");
                return thread1.getName();
            }
        });
        try {
            System.out.println(future.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
         */

        Future<String> submit = executorService.submit(() -> {
            Thread thread1 = new Thread();
            thread1.setName("aaa");
            return thread1.getName();
        });

        try {
            System.out.println(submit.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }


    }
}
